﻿namespace ufida.u9.ait.openapi
{
	using System;
    using System.Collections;
    using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.CBO.SCM.Item;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

    /// <summary>
    /// CommonUpdateSV partial 
    /// </summary>	
	public partial class CommonUpdateSV 
	{	
		internal BaseStrategy Select()
		{
			return new CommonUpdateSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class CommonUpdateSVImpementStrategy : BaseStrategy
	{
		public CommonUpdateSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			CommonUpdateSV bpObj = (CommonUpdateSV)obj;

            try
            {
                if (bpObj.Data == null||bpObj.Data.Datas.Length==0)
                    throw new Exception("参数为空");
                string entityName = bpObj.Data.EntityName;
                string entityCode = bpObj.Data.EntityCode;

                List<Dictionary<string, object>> datalist = JSONUtils.toMapList(bpObj.Data.Datas);
                List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();
                using (ISession session = Session.Open())
				{
                    EntityQuery query = new EntityQuery(entityName);
                    foreach (var datadto in datalist)
                    {
                        object code = datadto[entityCode];
                        if (code != null)
                        {
                            //弱类型 实体查询
                            IList entityList = query.FindAll($"{entityCode} = '{code.ToString()}'");
                            if (entityList != null && entityList.Count > 0)
                            {
                                //获取实体,转化为BusinessEntity
                                BusinessEntity entity = (BusinessEntity)entityList[0];
                                foreach (var col in datadto)
                                {
                                    //基于传入字段更新数据
                                    if (col.Key == entityCode) continue;
                                    //弱类型赋值
                                    entity.SetValue(col.Key, col.Value);
                                }

                                //处理返回值
                                retlist.Add(new CommonArchiveDataDTOData()
                                {
                                    ID = entity.ID,
                                    Code = entity[entityCode].ToString(),
                                });
                            }
                        }
                    }
                    
                    session.Commit();
                }
                
                return ResultUtils.success(retlist);
            }
            catch (Exception ex)
            {
                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
        }		
	}

	#endregion
	
	
}